package com.site.wisdom_construction.controller;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.entity.iotModule.AiMonitoring;
import com.site.wisdom_construction.entity.iotModule.RealTimePerson;
import com.site.wisdom_construction.entity.qualityModule.QualityDangerRecord;
import com.site.wisdom_construction.entity.safetyModule.SafetyDangerRecord;
import com.site.wisdom_construction.entity.safetyModule.SafetyInspection;
import com.site.wisdom_construction.service.iotModule.AiMonitoringService;
import com.site.wisdom_construction.service.iotModule.RealTimePersonService;
import com.site.wisdom_construction.service.personnelModule.ContractWorkersService;
import com.site.wisdom_construction.service.qualityModule.QualityDangerRecordService;
import com.site.wisdom_construction.service.safetyModule.SafetyDangerRecordService;
import com.site.wisdom_construction.service.safetyModule.SafetyInspectionService;
import com.site.wisdom_construction.util.HttpRequest;
import com.site.wisdom_construction.util.QrCodeUtils;
import com.site.wisdom_construction.util.TimeUtils;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author daiyifei @Date 2022/6/15 @Description
 */
@Api(tags = "测试数据推送")
@RestController
@RequestMapping("TestPush")
@CrossOrigin
public class PushDataController {
  @Autowired private AiMonitoringService aiMonitoringService;

  @Autowired private RealTimePersonService realTimePersonService;

  @Autowired private SafetyInspectionService safetyInspectionService;

  @Autowired private SafetyDangerRecordService safetyDangerRecordService;

  @Autowired private QualityDangerRecordService qualityDangerRecordService;

  @Autowired private ContractWorkersService contractWorkersService;

  @GetMapping(value = "/dataAi")
  public void pushData() {
    Map map = new HashMap();
    map.put("projectId", 1);
    List<AiMonitoring> aiMonitoringList = aiMonitoringService.selectAiMonitoring(map);
    HttpRequest httpRequest = new HttpRequest();
    String url = "https://jgpt.xzdtjt.com:8443/api/external/monitor-event";
    JSONObject jsonObject = new JSONObject();
    JSONObject jsonObjectR = new JSONObject();
    jsonObject.put("device_id", "70452d2a-eba2-4aab-b941-17d8c8cc1955");
    jsonObject.put("type", 1);
    for (int i = 0; i < aiMonitoringList.size(); i++) {
      AiMonitoring aiMonitoring = aiMonitoringList.get(i);
      String time = String.valueOf(aiMonitoring.getSnapTime());
      jsonObject.put("timestamp", TimeUtils.dateToStamp(time));
      jsonObject.put("action", aiMonitoring.getSnapType());
      jsonObjectR = httpRequest.PushData(url, jsonObject);
    }
  }

  @GetMapping(value = "/dataPerson")
  public void pushDataPerson() {
    Map map = new HashMap();
    map.put("projectId", 1);
    List<RealTimePerson> realTimePersonList = realTimePersonService.findRealTimePersonAll(map);
    System.out.println("size:" + realTimePersonList.size());
  }

  @PostMapping(value = "/dataPersons")
  @NoToken
  public void psss(@RequestBody Map map) {
    QrCodeUtils qrCodeUtils = new QrCodeUtils();
    String s = qrCodeUtils.drawingQrCode(map);
    System.out.println("s:" + s);
  }

  @PostMapping(value = "/Test")
  @NoToken
  public void Test(@RequestBody Map map) {
    System.out.println("map:" + map);
  }

  /** 智慧工地对接智慧安监平台-隧道局 */
  @ApiOperation(value = "巡检点数据上传")
  @PostMapping(value = "/InspectionPoint")
  @NoToken
  public void InspectionPoint(@RequestBody Map map) {
    map.put("projectId", 2);
    List<SafetyInspection> safetyInspectionList =
        safetyInspectionService.selectSafetyInspection(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/InspectionPoint";
    jsonObject.put("belongedTo", "AJ320301-1");
    jsonObject.put("recordNumber", "3203011D610101");
    String uuid = "8c184c2dc54245eaae13693ab5b678bb";
    for (int i = 0; i < safetyInspectionList.size(); i++) {
      jsonObject.put("inspectionId", "3203011D610101" + uuid);
      jsonObject.put("site", safetyInspectionList.get(i).getInspectionName());
      JSONObject jsonObjectL = httpRequest.pushJsonData(path, jsonObject);
      System.out.println("jsonObjectL:" + jsonObjectL);
    }
  }

  /** 智慧工地对接智慧安监平台-隧道局 */
  @ApiOperation(value = "巡检内容数据上传")
  @PostMapping(value = "/InspectionPointContent")
  @NoToken
  public void InspectionPointContent(@RequestBody Map map) {
    map.put("projectId", 2);
    List<SafetyDangerRecord> safetyDangerRecordList =
        safetyDangerRecordService.selectSafetyDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/InspectionPointContent";
    jsonObject.put("belongedTo", "AJ320301-1");
    jsonObject.put("recordNumber", "3203011D610101");
    String uuid = "8c184c2dc54245eaae13693ab5b678bb";
    for (int i = 0; i < safetyDangerRecordList.size(); i++) {
      jsonObject.put("inspectionId", "3203011D610101" + uuid);
      jsonObject.put("inspectionContentId", uuid);
      String inspectionTime =
          String.valueOf(safetyDangerRecordList.get(i).getDiscoverTime()) + " 00:00:00";
      jsonObject.put("inspectionTime", inspectionTime);
      jsonObject.put("checkPerson", safetyDangerRecordList.get(i).getManagerialName());
      jsonObject.put("checkContent", safetyDangerRecordList.get(i).getSafetyDescription());
      jsonObject.put("rectifyPerson", safetyDangerRecordList.get(i).getWorkersName());
      jsonObject.put("rectifyDate", inspectionTime);
      String[] strings = {"1"};
      strings[0] = safetyDangerRecordList.get(i).getSafetyImg();
      jsonObject.put("urls", strings);
      System.out.println("jsonObject:" + jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path, jsonObject);
      System.out.println("jsonObjectL:" + jsonObjectL);
    }
  }

  /** 智慧工地对接智慧安监平台-隧道局 */
  @ApiOperation(value = "巡检整改回复")
  @PostMapping(value = "/RectifyInspectionPointContent")
  @NoToken
  public void RectifyInspectionPointContent(@RequestBody Map map) {
    map.put("projectId", 2);
    List<SafetyDangerRecord> safetyDangerRecordList =
        safetyDangerRecordService.selectSafetyDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/RectifyInspectionPointContent";
    jsonObject.put("belongedTo", "AJ320301-1");
    jsonObject.put("recordNumber", "3203011D610101");
    String uuid = "8c184c2dc54245eaae13693ab5b678bb";
    for (int i = 0; i < safetyDangerRecordList.size(); i++) {
      jsonObject.put("inspectionId", "3203011D610101" + uuid);
      jsonObject.put("inspectionContentId", uuid);
      String finalRectifyDate =
          String.valueOf(safetyDangerRecordList.get(i).getAbarbeitungTime()) + " 00:00:00";
      jsonObject.put("finalRectifyDate", finalRectifyDate);
      jsonObject.put("rectifyContents", safetyDangerRecordList.get(i).getAbarbeitungDescription());
      String[] strings = {"1"};
      strings[0] = safetyDangerRecordList.get(i).getAbarbeitungImg();
      jsonObject.put("rectifyUrls", strings);
      System.out.println("jsonObject:" + jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path, jsonObject);
      System.out.println("jsonObjectL:" + jsonObjectL);
      System.out.println("请求接口次数:" + i);
    }
  }

  /** 智慧工地对接智慧安监平台-隧道局 */
  @ApiOperation(value = "隐患随手拍数据上传")
  @PostMapping(value = "/shootContent")
  @NoToken
  public void shootContent(@RequestBody Map map) {
    map.put("projectId", 2);
    List<SafetyDangerRecord> safetyDangerRecordList =
        safetyDangerRecordService.selectSafetyDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/shootContent";
    jsonObject.put("belongedTo", "AJ320301-1");
    jsonObject.put("recordNumber", "3203011D610101");
    String uuid = "8c184c2dc54245eaae13693ab5b678bb";
    for (int i = 0; i < safetyDangerRecordList.size(); i++) {
      jsonObject.put("shootId", uuid);
      String inspectionTime =
          String.valueOf(safetyDangerRecordList.get(i).getDiscoverTime()) + " 00:00:00";
      jsonObject.put("shootTime", inspectionTime);
      jsonObject.put("shootPerson", safetyDangerRecordList.get(i).getManagerialName());
      jsonObject.put("shootAdress", safetyDangerRecordList.get(i).getSafetyInspectionName());
      jsonObject.put("shotContent", safetyDangerRecordList.get(i).getSafetyDescription());
      jsonObject.put("rectifyPerson", safetyDangerRecordList.get(i).getWorkersName());
      jsonObject.put("rectifyDate", inspectionTime);
      String[] strings = {"1"};
      strings[0] = safetyDangerRecordList.get(i).getSafetyImg();
      jsonObject.put("urls", strings);
      System.out.println("jsonObject:" + jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path, jsonObject);
      System.out.println("jsonObjectL:" + jsonObjectL);
    }
  }

  /** 智慧工地对接智慧安监平台-隧道局 */
  @ApiOperation(value = "随手拍整改回复内容上传")
  @PostMapping(value = "/RectifyShootContent")
  @NoToken
  public void RectifyShootContent(@RequestBody Map map) {
    map.put("projectId", 2);
    List<SafetyDangerRecord> safetyDangerRecordList =
        safetyDangerRecordService.selectSafetyDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/RectifyShootContent";
    jsonObject.put("belongedTo", "AJ320301-1");
    jsonObject.put("recordNumber", "3203011D610101");
    String uuid = "8c184c2dc54245eaae13693ab5b678bb";
    for (int i = 0; i < safetyDangerRecordList.size(); i++) {
      jsonObject.put("shootId", uuid);
      String finalRectifyDate =
          String.valueOf(safetyDangerRecordList.get(i).getAbarbeitungTime()) + " 00:00:00";
      jsonObject.put("finalRectifyDate", finalRectifyDate);
      jsonObject.put("rectifyContents", safetyDangerRecordList.get(i).getAbarbeitungDescription());
      String[] strings = {"1"};
      strings[0] = safetyDangerRecordList.get(i).getAbarbeitungImg();
      jsonObject.put("rectifyUrls", strings);
      System.out.println("jsonObject:" + jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path, jsonObject);
      System.out.println("jsonObjectL:" + jsonObjectL);
      System.out.println("请求接口次数:" + i);
    }
  }

  /** 智慧工地对接智慧安监平台-隧道局 */
  @ApiOperation(value = "检查单数据上传")
  @PostMapping(value = "/InspectContentInfo")
  @NoToken
  public void InspectContentInfo(@RequestBody Map map) {
    map.put("projectId", 2);
    List<QualityDangerRecord> qualityDangerRecordList =
        qualityDangerRecordService.selectQualityDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/InspectContentInfo";
    jsonObject.put("belongedTo", "AJ320301-1");
    jsonObject.put("recordNumber", "3203011D610101");
    String times = String.valueOf(TimeUtils.getStampH());
    jsonObject.put("checkNumber", "3203011D610101" + 1667268646);
    jsonObject.put("checkNumType", 1);
    jsonObject.put("idcard", "410223199405114552");
    jsonObject.put("checkPerson", "张文龙");
    jsonObject.put("isRectify", 1);
    for (int i = 0; i < qualityDangerRecordList.size(); i++) {
      String finalRectifyDate =
          String.valueOf(qualityDangerRecordList.get(i).getDiscoverTime()) + " 00:00:00";
      jsonObject.put("checkDate", finalRectifyDate);
      jsonObject.put("checkContent", qualityDangerRecordList.get(i).getQualityDescription());
      String[] strings = {"1"};
      strings[0] = qualityDangerRecordList.get(i).getQualityImg();
      jsonObject.put("urls", strings);
      jsonObject.put("rectifyPerson", qualityDangerRecordList.get(i).getWorkersName());
      jsonObject.put("rectifyDate", finalRectifyDate);
      System.out.println("jsonObject:" + jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path, jsonObject);
      System.out.println("jsonObjectL:" + jsonObjectL);
      System.out.println("请求接口次数:" + i);
    }
  }

  /** 智慧工地对接智慧安监平台-隧道局 */
  @ApiOperation(value = "检查单整改完成数据上传")
  @PostMapping(value = "/RectifyContentInfo")
  @NoToken
  public void RectifyContentInfo(@RequestBody Map map) {
    map.put("projectId", 2);
    List<QualityDangerRecord> qualityDangerRecordList =
        qualityDangerRecordService.selectQualityDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/RectifyContentInfo";
    jsonObject.put("belongedTo", "AJ320301-1");
    jsonObject.put("recordNumber", "3203011D610101");
    String times = String.valueOf(TimeUtils.getStampH());
    jsonObject.put("checkNumber", "3203011D610101" + 1667268646);
    for (int i = 0; i < qualityDangerRecordList.size(); i++) {
      String finalRectifyDate =
          String.valueOf(qualityDangerRecordList.get(i).getAbarbeitungTime()) + " 00:00:00";
      jsonObject.put("finalRectifyDate", finalRectifyDate);
      jsonObject.put("rectifyContents", qualityDangerRecordList.get(i).getAbarbeitungDescription());
      String[] strings = {"1"};
      strings[0] = qualityDangerRecordList.get(i).getAbarbeitungImg();
      jsonObject.put("rectifyUrls", strings);
      System.out.println("jsonObject:" + jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path, jsonObject);
      System.out.println("jsonObjectL:" + jsonObjectL);
      System.out.println("请求接口次数:" + i);
    }
  }

  @ApiOperation(value = "仙居测试")
  @PostMapping(value = "/insertPerson")
  @NoToken
  public void insertPerson(@RequestBody Map map) {
    for (int i = 0; i < 500; i++) {
      contractWorkersService.insertContractWorkersByMap(map);
    }
  }
}
